<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: LayoutProperty.php 425 2011-01-05 10:30:30Z steen.rabol $
 *
 */

Prado::using('Application.Common.Data.PortalPageRecord');
Prado::using('Application.Common.Data.PortalThemeRecord');
Prado::using('Application.Common.Data.PortalThemeCssClassRecord');

class LayoutProperty extends PortalSystemPage
{
	private $_pagename		= '';
	private $_controlname	= '';
	private $_portletname	= '';
	private $_recid			= 0;
	private	$_pagerecord	= null;


	public function onInit($param)
	{
		parent::onInit($param);

		if(!$this->User)
		{
			$this->reportError(1,Prado::localize("You need to be logged in to use this page"));
		}

		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			$this->reportError(1,Prado::localize("You do not have access to this page or feature"));
		}

		$this->ControlName	= $this->Request['cid'];
		$this->PageName		= $this->Request['pid'];

		$name		= $this->PageName . '_' . $this->ControlName;
		$lConfig	= PortalConfigRecord::finder()->findByname($name);
		$themename	= $this->Application->Parameters['ThemeName'];
		$themeRec	= PortalThemeRecord::finder()->findByname($themename);
		$this->dlCssSelect->DataSource	= PortalThemeCssClassRecord::finder()->findAllBythemeid($themeRec->id);
		$this->dlCssSelect->databind();

		if($lConfig)
		{
			$p = Prado::unserialize($lConfig->value);

			if(isset($p["color"]))
			{
				$this->TextColor->Text = $p["color"];
			}

			if(isset($p["background-color"]))
			{
				$this->BackgroundColor->Text	= $p["background-color"];
			}

			if(isset($p["class"]) && !empty($p['class']))
			{
				$this->dlCssSelect->SelectedValue	= $p["class"];
			}
		}
	}

	public function getControlName()
	{
		return $this->_controlname;
	}

	public function setControlName($value)
	{
		$this->_controlname	= $value;
	}

	public function getPageName()
	{
		return $this->_pagename;
	}

	public function setPageName($value)
	{
		$this->_pagename	= $value;
	}

	public function onLoad($param)
	{
		$this->Title = $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Page properties");
	}

	public function saveButtonClicked($sender,$param)
	{
		if($this->Page->IsValid)
		{
			$name	= $this->PageName . '_' . $this->ControlName;
			$lConfig	= PortalConfigRecord::finder()->findByname($name);
			if(!$lConfig)
			{
				$lConfig = new PortalConfigRecord();
				$lConfig->name	= $name;
			}

			$p = array();
			$p["color"]				= $this->TextColor->Text;
			$p["background-color"]	= $this->BackgroundColor->Text;
			$p["style"]				= ($this->TextColor->Text !="" ? "color:" . $this->TextColor->Text : "");
			$p["style"]				.= ";";
			$p["style"]				.= ($this->BackgroundColor->Text !="" ? "background-color:" . $this->BackgroundColor->Text : "");
			$p["style"]				.= ";";
			$p["class"]				=  $this->dlCssSelect->SelectedValue;

			$lConfig->value	= Prado::serialize($p);
			$lConfig->save();
			$this->gotoPage($this->PageName,array("PageMode" => $this->Page->EditMode));
		}
	}

	public function cancelButtonClicked($sender,$param)
	{
		$this->gotoPage($this->PageName,array("PageMode" => $this->Page->EditMode));
	}
}
?>